//
//  LYSearchFriendViewController.m
//  artist
//
//  Created by laotang on 16/5/11.
//  Copyright © 2016年 viviyoo. All rights reserved.
//

#import "LYSearchFriendViewController.h"

#import "LYChatFriendsModel.h"
#import "SessionViewCell.h"

@interface LYSearchFriendViewController ()<UISearchBarDelegate>
@property (nonatomic, strong) UISearchDisplayController *searchDisplayController;
@property (nonatomic, strong) NSArray *chatArray;
@property (nonatomic, strong) UISearchBar *searchBar;

@end

@implementation LYSearchFriendViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [super viewDidLoad];
    UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)];
    self.searchBar = searchBar;
    searchBar.placeholder = @"搜索";
    searchBar.delegate = self;
    // 添加 searchbar 到 headerview
    self.tableView.tableHeaderView = searchBar;
    
    // 用 searchbar 初始化 SearchDisplayController
    // 并把 searchDisplayController 和当前 controller 关联起来
    self.searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self];
    
    // searchResultsDataSource 就是 UITableViewDataSource
    self.searchDisplayController.searchResultsDataSource = self;
    // searchResultsDelegate 就是 UITableViewDelegate
    self.searchDisplayController.searchResultsDelegate = self;
}

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
    
    WS(weakSelf)
    
    NSLog(@"%@", [AppDelegate shareInstance].token);
    [LYRequestTool searchFriendWithToken:[AppDelegate shareInstance].token keyword:searchBar.text success:^(NSArray<LYSearchFriendModel *> *friendArray) {
        weakSelf.chatArray = friendArray;
        [weakSelf.tableView reloadData];
//        weakSelf.searchDisplayController.se
        
        weakSelf.searchDisplayController.active = NO;
        [weakSelf.searchBar resignFirstResponder];
    } failure:^(NSString *code, NSString *message) {
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.chatArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellId = @"mycell";
    SessionViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    
    if (cell == nil) {
        cell = [[SessionViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
    }
    
    LYSearchFriendModel *friend = self.chatArray[indexPath.row];
    [cell.portraitImg sd_setImageWithURL:[NSURL URLWithString:friend.head_pic]];
    cell.portraitImg.layer.cornerRadius = 20;
    cell.portraitImg.layer.borderColor = [UIColor whiteColor].CGColor;
    cell.portraitImg.layer.borderWidth = 2;
    cell.portraitImg.layer.masksToBounds = YES;
    cell.nameLabel.text = friend.nick_name;
    cell.unReadLabel.hidden = YES;
    cell.noPushImg.hidden = YES;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 65;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    LYSearchFriendModel *friend = self.chatArray[indexPath.row];

    [LYRequestTool addFriendssWithToken:[AppDelegate shareInstance].token userID:friend.user_id success:^{
        NSString *success = [NSString stringWithFormat:@"添加 %@ 成功",friend.nick_name];
        [MBProgressHUD showSuccess:success];
    } failure:^(NSString *code, NSString *message) {
        [MBProgressHUD showError:message];
        
    }];
}
@end
